<template>
  <div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar :title="comment.reply_count+'条回复'">
      <van-icon slot="left" name="cross" @click="$emit('close')" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论">
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        :src="comment.aut_photo"
      />
      <span slot="title" style="color: #466b9d;">{{ comment.aut_name }}</span>
      <div slot="label">
        <p style="color: #363636;">{{ comment.content }}</p>
        <p>
          <span style="margin-right: 10px;">{{ comment.pubdate }}</span>
          <van-button
            size="mini"
            type="default"
          >回复{{ comment.reply_count }}</van-button>
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
    <!-- /当前评论 -->
    <van-divider>全部回复</van-divider>

    <!-- 对当前评论 回复列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate }}</span>
          </p>
        </div>
        <van-icon slot="right-icon" :name="item.is_liking?'like':'like-o'" @click="isLikeRelay(item)" />
      </van-cell>
    </van-list>
    <!-- 回复列表 -->

    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="content"
        clearable
        placeholder="请输入回复内容"
      >
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="addCommentReplay"
        >发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
  </div>

</template>
<script>
import { reqAddReplay, reqGetCommentReplay, reqCommentLike, reqDelCommentLike } from '@/api/comments'
export default {
  name: 'CommentReply',
  props: {
    comment: {
      type: Object,
      required: true
    },
    articleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束,
      content: '',
      offset: null
    }
  },
  methods: {
    async  onLoad() {
      const { data } = await reqGetCommentReplay(this.comment.com_id, this.offset)
      console.log(data)
      this.list.push(...data.results)
      this.offset = data.last_id
      this.loading = false
      if (data.results.length === 0) {
        this.finished = true
      }
    },
    async  addCommentReplay() {
      const { data, data: { new_obj }} = await reqAddReplay(this.comment.com_id, this.content, this.articleId)
      console.log(data)
      this.$toast.success('回复评论成功')
      this.list.unshift(new_obj)
    },
    async isLikeRelay(item) {
      if (item.is_liking) {
        await reqDelCommentLike(item.com_id)
        this.$toast.success('取消点赞成功')
      } else {
        await reqCommentLike(item.com_id)
        this.$toast.success('点赞成功')
      }
      item.is_liking = !item.is_liking
    }
  }
}

</script>
<style lang="scss" scoped>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
